<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{number}}.{{title}} - MyOJ</title>

    <!-- ACE & jQuery 保持你原来 CDN -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.23.4/ace.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.23.4/ext-language_tools.min.js"></script>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

    <style>
        /*========= 设计变量 =========*/
        :root {
            --primary: #00c896;
            --primary-dark: #009e78;
            --bg-start: #0f172a;
            --bg-end: #1e293b;
            --glass: rgba(30, 41, 59, .55);
            --radius: 12px;
            --shadow: 0 8px 32px rgba(0, 0, 0, .37);
            --font-color: #e2e8f0;
            --font-dark: #94a3b8;
            --success: #4ade80;
            --error: #f472b6;
        }

        /*========= 通用 reset =========*/
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        html, body {
            width: 100%;
            height: 100%;
            font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
            color: var(--font-color);
            background: linear-gradient(135deg, var(--bg-start), var(--bg-end));
            background-attachment: fixed;
        }
        a {
            color: inherit;
            text-decoration: none;
        }

        /*========= 粒子背景 =========*/
        #particles {
            position: fixed;
            inset: 0;
            z-index: -1;
        }

        /*========= 导航栏（与首页一致） =========*/
        header {
            position: sticky;
            top: 0;
            height: 64px;
            background: var(--glass);
            backdrop-filter: blur(12px);
            -webkit-backdrop-filter: blur(12px);
            border-bottom: 1px solid rgba(255, 255, 255, .05);
            z-index: 1000;
        }
        nav {
            max-width: 1200px;
            margin: 0 auto;
            height: 100%;
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 0 24px;
        }
        .logo {
            font-size: 22px;
            font-weight: 700;
            letter-spacing: 1px;
            color: var(--primary);
        }
        .nav-links {
            display: flex;
            gap: 32px;
            align-items: center;
        }
        .nav-links a {
            color: var(--font-dark);
            font-size: 15px;
            transition: color .2s;
        }
        .nav-links a:hover {
            color: var(--primary);
        }
        .nav-btn {
            background: var(--primary);
            color: #fff;
            padding: 8px 18px;
            border-radius: var(--radius);
            font-weight: 600;
            transition: background .2s;
        }
        .nav-btn:hover {
            background: var(--primary-dark);
        }
        .hamburger {
            display: none;
            flex-direction: column;
            gap: 5px;
            cursor: pointer;
        }
        .hamburger span {
            width: 24px;
            height: 2px;
            background: var(--font-color);
            transition: .3s;
        }

        /*========= 分栏布局 =========*/
        .wrapper {
            display: flex;
            height: calc(100vh - 64px);
            gap: 12px;
            padding: 12px;
        }
        .pane {
            background: var(--glass);
            backdrop-filter: blur(12px);
            border: 1px solid rgba(255, 255, 255, .06);
            border-radius: var(--radius);
            box-shadow: var(--shadow);
            overflow: hidden;
            display: flex;
            flex-direction: column;
        }
        .left-pane {
            flex: 1 1 45%;
            padding: 24px;
        }
        .right-pane {
            flex: 1 1 55%;
            position: relative;
        }
        .pane-header {
            font-size: 18px;
            font-weight: 600;
            margin-bottom: 12px;
            color: var(--primary);
        }
        .desc-content {
            flex: 1;
            overflow: auto;
            font-size: 15px;
            line-height: 1.7;
            white-space: pre-wrap;
        }
        #editor {
            width: 100%;
            height: 100%;
        }

        /*========= 拖拽条 =========*/
        .gutter {
            width: 6px;
            background: rgba(255, 255, 255, .08);
            cursor: col-resize;
            border-radius: 3px;
            transition: background .2s;
        }
        .gutter:hover,
        .gutter.dragging {
            background: var(--primary);
        }

        /*========= 结果区 =========*/
        .bottom-bar {
            height: 140px;
            background: var(--glass);
            backdrop-filter: blur(12px);
            border-top: 1px solid rgba(255, 255, 255, .06);
            display: flex;
            flex-direction: column;
            padding: 12px 24px;
        }
        .result-header {
            display: flex;
            align-items: center;
            justify-content: space-between;
            margin-bottom: 8px;
        }
        .status-label {
            padding: 4px 10px;
            border-radius: 999px;
            font-size: 13px;
            font-weight: 600;
            color: #000;
        }
        .status-label.success {
            background: var(--success);
        }
        .status-label.error {
            background: var(--error);
        }
        .copy-btn {
            background: rgba(255, 255, 255, .1);
            border: 0;
            color: var(--font-color);
            padding: 4px 10px;
            border-radius: var(--radius);
            cursor: pointer;
            font-size: 13px;
        }
        .copy-btn:hover {
            background: rgba(255, 255, 255, .2);
        }
        pre.result {
            flex: 1;
            overflow: auto;
            font-size: 14px;
            line-height: 1.5;
            margin: 0;
            padding: 8px;
            background: rgba(0, 0, 0, .15);
            border-radius: var(--radius);
        }

        /*========= 提交按钮 =========*/
        .submit-btn {
            position: absolute;
            right: 16px;
            bottom: 16px;
            background: var(--primary);
            color: #fff;
            padding: 10px 24px;
            border: 0;
            border-radius: var(--radius);
            font-size: 15px;
            font-weight: 600;
            cursor: pointer;
            transition: background .2s, transform .2s;
            box-shadow: var(--shadow);
        }
        .submit-btn:hover {
            background: var(--primary-dark);
            transform: translateY(-2px);
        }

        /*========= 响应式 =========*/
        @media (max-width: 900px) {
            .wrapper {
                flex-direction: column;
            }
            .gutter {
                display: none;
            }
            .left-pane,
            .right-pane {
                flex: 1 1 auto;
            }
        }
        @media (max-width: 768px) {
            .nav-links {
                position: absolute;
                top: 64px;
                right: 24px;
                flex-direction: column;
                background: var(--glass);
                backdrop-filter: blur(12px);
                border-radius: var(--radius);
                padding: 20px 24px;
                gap: 20px;
                opacity: 0;
                pointer-events: none;
                transform: translateY(-10px);
                transition: .3s;
            }
            .nav-links.active {
                opacity: 1;
                pointer-events: auto;
                transform: translateY(0);
            }
            .hamburger {
                display: flex;
            }
        }
    </style>
</head>
<body>
    <canvas id="particles"></canvas>

    <!-- 导航栏 -->
    <header>
        <nav>
            <div class="logo">MyOJ</div>
            <div class="nav-links" id="navLinks">
                <a href="/">首页</a>
                <a href="/all_questions">题库</a>
                <a href="#">竞赛</a>
                <a href="#">讨论</a>
                <a href="#">求职</a>
                <a class="nav-btn" href="#">登录</a>
                <a class="nav-btn" href="#">注册</a>
            </div>
            <div class="hamburger" id="hamburger">
                <span></span><span></span><span></span>
            </div>
        </nav>
    </header>

    <div class="wrapper">
        <!-- 左侧题目描述 -->
        <div class="pane left-pane">
            <div class="pane-header">{{number}}. {{title}}</div>
            <div class="desc-content">{{desc}}</div>
        </div>

        <!-- 拖拽条 -->
        <div class="gutter" id="gutter"></div>

        <!-- 右侧代码区 -->
        <div class="pane right-pane">
            <!-- 1. 隐藏 textarea 保存后端给的模板代码 -->
            <textarea id="preCode" style="display:none;">{{pre_code}}</textarea>

            <!-- 2. ACE 容器，保持空 -->
            <pre id="editor"></pre>
            <button class="submit-btn" onclick="submit()">提交</button>
        </div>
    </div>

    <!-- 结果区 -->
    <div class="bottom-bar">
        <div class="result-header">
            <span class="status-label" id="status" style="display:none;"></span>
            <!-- <button class="copy-btn" id="copyBtn" style="display:none;">复制输出</button> -->
        </div>
        <pre class="result" id="result"></pre>
    </div>

    <script>
        /* ========= 粒子背景（复用） ========= */
        const canvas = document.getElementById('particles');
        const ctx = canvas.getContext('2d');
        let particlesArray;
        function resize() {
            canvas.width = window.innerWidth;
            canvas.height = window.innerHeight;
        }
        window.addEventListener('resize', resize);
        resize();
        class Particle {
            constructor() {
                this.x = Math.random() * canvas.width;
                this.y = Math.random() * canvas.height;
                this.size = Math.random() * 1.5 + 1;
                this.speedX = (Math.random() - 0.5) * 0.4;
                this.speedY = (Math.random() - 0.5) * 0.4;
            }
            update() {
                this.x += this.speedX;
                this.y += this.speedY;
                if (this.x < 0 || this.x > canvas.width) this.speedX *= -1;
                if (this.y < 0 || this.y > canvas.height) this.speedY *= -1;
            }
            draw() {
                ctx.fillStyle = 'rgba(0, 200, 150, 0.55)';
                ctx.beginPath();
                ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
                ctx.fill();
            }
        }
        function initParticles() {
            particlesArray = [];
            for (let i = 0; i < 70; i++) particlesArray.push(new Particle());
        }
        initParticles();
        function animateParticles() {
            ctx.clearRect(0, 0, canvas.width, canvas.height);
            for (let p of particlesArray) {
                p.update();
                p.draw();
            }
            requestAnimationFrame(animateParticles);
        }
        animateParticles();

        /* ========= 导航菜单 ========= */
        const hamburger = document.getElementById('hamburger');
        const navLinks = document.getElementById('navLinks');
        hamburger.addEventListener('click', () => navLinks.classList.toggle('active'));

        /* ========= ACE 编辑器 ========= */
        const editor = ace.edit("editor");
        editor.setTheme("ace/theme/monokai");
        editor.session.setMode("ace/mode/c_cpp");
        editor.setFontSize(16);
        editor.getSession().setTabSize(4);
        editor.setReadOnly(false);
        ace.require("ace/ext/language_tools");
        editor.setOptions({
            enableBasicAutocompletion: true,
            enableSnippets: true,
            enableLiveAutocompletion: true
        });
        const preCode = document.getElementById('preCode').value.trim();
        editor.setValue(preCode, -1);   // -1 把光标放到开头

        /* ========= 拖拽分栏 ========= */
        const gutter = document.getElementById('gutter');
        const leftPane = document.querySelector('.left-pane');
        const wrapper = document.querySelector('.wrapper');
        let isDragging = false;
        gutter.addEventListener('mousedown', (e) => {
            isDragging = true;
            gutter.classList.add('dragging');
            document.body.style.cursor = 'col-resize';
        });
        document.addEventListener('mousemove', (e) => {
            if (!isDragging) return;
            const wrapperRect = wrapper.getBoundingClientRect();
            const percent = ((e.clientX - wrapperRect.left) / wrapperRect.width) * 100;
            if (percent < 20 || percent > 80) return;
            leftPane.style.flexBasis = `${percent}%`;
        });
        document.addEventListener('mouseup', () => {
            isDragging = false;
            gutter.classList.remove('dragging');
            document.body.style.cursor = 'default';
        });

        /* ========= 提交 & 结果回显 ========= */
        function submit() {
            const code = editor.getValue();
            const number = '{{number}}';
            const judge_url = `/judge/${number}`;

            $.ajax({
                method: 'POST',
                url: judge_url,
                dataType: 'json',
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify({
                    "code": code,
                    "input": ""
                }),
                success: function (data) {
                    showResult(data);
                }
            });
        }

        function showResult(data) {
            const statusEl = document.getElementById('status');
            const resultEl = document.getElementById('result');

            /* 清空旧结果 */
            resultEl.textContent = '';
            statusEl.style.display = 'inline-block';

            const { status, reason, stdout = '', stderr = '' } = data;
            statusEl.textContent = reason;
            statusEl.className = 'status-label ' + (status === 0 ? 'success' : 'error');

            /* 用 innerHTML 渲染，保留换行与空格 */
            const output = (status === 0 ? stdout : stderr)
                        .replace(/\n/g, '<br>')      // 换行 → <br>
                        .replace(/ /g, '&nbsp;');   // 空格 → &nbsp;

            resultEl.innerHTML = output;
        }
    </script>
</body>
</html>